// app/model/contact_person_model.js
'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, DATE } = app.Sequelize;

  const ContactPerson = app.model.define('contactPerson', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    customerId: {
      type: INTEGER,
      allowNull: false,
      field: 'customer_id',
      comment: '客户ID'
    },
    name: {
      type: STRING(100),
      allowNull: false,
      comment: '联系人姓名'
    },
    department: {
      type: STRING(100),
      comment: '部门'
    },
    position: {
      type: STRING(100),
      comment: '职位'
    },
    phone: {
      type: STRING(20),
      comment: '电话'
    },
    email: {
      type: STRING(100),
      comment: '邮箱'
    },
    address: {
      type: STRING(200),
      comment: '地址'
    },
    isPrimary: {
      type: INTEGER,
      defaultValue: 0,
      field: 'is_primary',
      comment: '是否为主要联系人: 1是, 0否'
    },
    remark: {
      type: TEXT,
      comment: '备注'
    },
    createdAt: {
      type: DATE,
      field: 'created_at'
    },
    updatedAt: {
      type: DATE,
      field: 'updated_at'
    }
  }, {
    tableName: 'contact_persons',
    timestamps: true,
    underscored: true
  });

  // 添加关联关系
  ContactPerson.associate = function() {
    // 联系人与客户的关联
    if (app.model.CustomerModel) {
      ContactPerson.belongsTo(app.model.CustomerModel, {
        foreignKey: 'customerId',
        as: 'customer'
      });
    }
  };

  return ContactPerson;
};